package com.mmlc.meimei.lichengkongjian.mine;

import android.content.Intent;
import android.graphics.BitmapFactory;
import android.text.TextUtils;
import android.view.View;
import android.widget.Button;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.RelativeLayout;
import android.widget.TextView;

import com.androidnetframe.BaseParentActivity;
import com.androidnetframe.httputil.BaseRequestFilterLayer;
import com.androidnetframe.responsejavabean.RequestError;
import com.androidnetframe.utils.UIUtils;
import com.easemob.helpdeskdemo.Constant;
import com.makeramen.roundedimageview.RoundedImageView;
import com.mmlc.inter.InterFragment;
import com.mmlc.inter.MyReceiver;
import com.mmlc.meimei.lichengkongjian.App;
import com.mmlc.meimei.lichengkongjian.R;
import com.mmlc.meimei.lichengkongjian.WDActivity;
import com.mmlc.meimei.lichengkongjian.WGActivity;
import com.mmlc.meimei.lichengkongjian.WPActivity;
import com.mmlc.meimei.lichengkongjian.classify.InformationSelectActivity;
import com.mmlc.meimei.lichengkongjian.home.WebViewActivity;
import com.mmlc.network.filter.UserDetailFilter;
import com.mmlc.network.responsebean.UserDetailResponseBean;
import com.mmlc.network.responsebean.UserEmpDetailResponseBean;
import com.mmlc.utils.PhoneUtils;
import com.mmlc.utils.ViewUtils;
import com.squareup.picasso.Picasso;

import butterknife.ButterKnife;
import butterknife.InjectView;
import butterknife.OnClick;

import static com.mmlc.meimei.lichengkongjian.R.id.iv_set;
import static com.mmlc.meimei.lichengkongjian.R.id.tv_account;


/**
 * Created by Administrator on 2016/7/19 0019.
 */
public class PersonCenterFragment extends InterFragment implements View.OnClickListener {

    @InjectView(R.id.iv_headImage)
    RoundedImageView ivHeadImage;
    @InjectView(tv_account)
    TextView tvAccount;
    @InjectView(R.id.rl_myInfo)
    RelativeLayout rlMyInfo;
    @InjectView(R.id.tv_money)
    TextView tvMoney;
    @InjectView(R.id.iv_left1)
    ImageView ivLeft1;
    @InjectView(R.id.tv_waitPay)
    TextView tvWaitPay;
    @InjectView(R.id.rl_1)
    RelativeLayout rl1;
    @InjectView(R.id.iv_left2)
    ImageView ivLeft2;
    @InjectView(R.id.tv_waitSend)
    TextView tvWaitSend;
    @InjectView(R.id.rl_2)
    RelativeLayout rl2;
    @InjectView(R.id.iv_left0)
    ImageView ivLeft0;
    @InjectView(R.id.tv_waitRecive)
    TextView tvWaitRecive;
    @InjectView(R.id.rl_3)
    RelativeLayout rl3;
    @InjectView(R.id.iv_left4)
    ImageView ivLeft4;
    @InjectView(R.id.tv_allOrder)
    TextView tvAllOrder;
    @InjectView(R.id.rl_4)
    RelativeLayout rl4;
    @InjectView(R.id.iv_left5)
    ImageView ivLeft5;
    @InjectView(R.id.tv_myLove)
    TextView tvMyLove;
    @InjectView(R.id.rl_5)
    RelativeLayout rl5;
    @InjectView(R.id.iv_left6)
    ImageView ivLeft6;
    @InjectView(R.id.tv_refund)
    TextView tvRefund;
    @InjectView(R.id.rl_6)
    RelativeLayout rl6;
    @InjectView(R.id.iv_left7)
    ImageView ivLeft7;
    @InjectView(R.id.tv_callWorker)
    TextView tvCallWorker;
    @InjectView(R.id.rl_7)
    RelativeLayout rl7;
    @InjectView(R.id.rootView)
    LinearLayout rootView;
    @InjectView(R.id.rl_8)
    RelativeLayout rl8;
    @InjectView(R.id.rl_9)
    RelativeLayout rl9;
    @InjectView(R.id.iv_left9)
    ImageView ivLeft9;
    @InjectView(R.id.tv_history)
    TextView tvHistory;
    @InjectView(R.id.rl_10)
    RelativeLayout rl10;
    @InjectView(R.id.iv_left8)
    ImageView mIvLeft8;
    @InjectView(R.id.tv_myHist)
    TextView mTvMyHist;
    @InjectView(R.id.iv_left11)
    ImageView mIvLeft11;
    @InjectView(R.id.tv_allseller)
    TextView mTvAllseller;
    @InjectView(R.id.rl_11)
    RelativeLayout mRl11;
    @InjectView(R.id.iv_left10)
    ImageView mIvLeft10;
    @InjectView(R.id.tv_addressManager)
    TextView mTvAddressManager;
    @InjectView(R.id.tv_waitPayNumber)
    TextView tvWaitPayNumber;
    @InjectView(R.id.tv_waitSendNumber)
    TextView tvWaitSendNumber;
    @InjectView(R.id.tv_waitReciveNumber)
    TextView tvWaitReciveNumber;
    @InjectView(R.id.rl_rank)
    RelativeLayout mRank;
    @InjectView(R.id.rl_customer)
    RelativeLayout rl_customer;
    @InjectView(R.id.iv_set)
    ImageView ivSet;
    @InjectView(R.id.iv_back)
    ImageView ivBack;
    @InjectView(R.id.rl_suggest)
    RelativeLayout rlSuggest;

    @InjectView(R.id.bt_detail)
    Button bt_detail;
    @InjectView(R.id.iv_myinfo)
    ImageView iv_myinfo;
    @InjectView(R.id.view_top)
    View view_top;
    @InjectView(R.id.view_customer)
    View view_customer;
    @InjectView(R.id.tv_login)
    TextView tv_login;
    @InjectView(R.id.ll_special)
    LinearLayout ll_special;
    @InjectView(R.id.rl_customer1)
    RelativeLayout rl_customer1;
    @InjectView(R.id.view_customer1)
    View view_customer1;

    @InjectView(R.id.rl_score)
    RelativeLayout rl_score;
    @InjectView(R.id.rl_vip)
    RelativeLayout rl_vip;

    private UserDetailResponseBean mUserBean;

    String url="";

    @Override
    protected int initResourceID() {
        return R.layout.fragment_personcenter;
    }

    @Override
    protected void initView() {
        ButterKnife.inject(this, view);
        ivBack.setVisibility(View.GONE);
        if(App.user.getLoginState()) {
            if (App.user.getUserType() == 0) {
                rlMyInfo.setClickable(false);
                tvMoney.setVisibility(View.GONE);
                bt_detail.setVisibility(View.GONE);
                iv_myinfo.setVisibility(View.GONE);
                ivSet.setVisibility(View.VISIBLE);
            } else {
                rlMyInfo.setClickable(true);
                tvMoney.setVisibility(View.VISIBLE);
                bt_detail.setVisibility(View.VISIBLE);
                iv_myinfo.setVisibility(View.VISIBLE);
                ivSet.setVisibility(View.VISIBLE);
            }
            if (!App.user.getLoginState()) {
                tvAccount.setText("取一个昵称吧");
            } else {
                initData();
            }
        }else{
            ViewUtils.getView(getActivity(), iv_set).setVisibility(View.GONE);
            rootView.setVisibility(View.GONE);
            tv_login.setVisibility(View.VISIBLE);
            ivSet.setVisibility(View.GONE);
        }
        firstHeadUrl = App.user.getPhoto();
    }

    public String firstHeadUrl="";

    @Override
    public void onResume() {
        super.onResume();
        if(App.user.getLoginState()) {
            if (App.user.getUserType() == 0) {
                rlMyInfo.setClickable(false);
                tvMoney.setVisibility(View.GONE);
                bt_detail.setVisibility(View.GONE);
                iv_myinfo.setVisibility(View.GONE);
            } else {
                rlMyInfo.setClickable(true);
                tvMoney.setVisibility(View.VISIBLE);
                bt_detail.setVisibility(View.VISIBLE);
                iv_myinfo.setVisibility(View.VISIBLE);
            }

            initData();
            ViewUtils.getView(getActivity(), iv_set).setVisibility(View.VISIBLE);
            rootView.setVisibility(View.VISIBLE);
            tv_login.setVisibility(View.GONE);
            ivSet.setVisibility(View.VISIBLE);
        }else{
            ViewUtils.getView(getActivity(), iv_set).setVisibility(View.GONE);
            rootView.setVisibility(View.GONE);
            tv_login.setVisibility(View.VISIBLE);
            ivSet.setVisibility(View.GONE);
        }
    }

    boolean goToLogin = false;

    @OnClick(R.id.tv_login)
    public void Login(){
        Intent intent = new Intent(getActivity(), LoginActivity.class);
        intent.putExtra("LoginOut","LoginOut");
        goToLogin = true;
        startActivity(intent);
    }

    public int is_store = 0 ,is_tuidan = 0;
    private void showOrderNumber() {
        tvWaitPayNumber.setVisibility(View.GONE);
        tvWaitSendNumber.setVisibility(View.GONE);
        tvWaitReciveNumber.setVisibility(View.GONE);

        UserDetailFilter filter = new UserDetailFilter((BaseParentActivity) getActivity());
        filter.requestBean.client_id = MyReceiver.getResId(getActivity());
        filter.requestBean.imei = PhoneUtils.getIMEI(getActivity());
        filter.sendRequest(new BaseRequestFilterLayer.AccessResultFromFilter<UserDetailResponseBean>() {

            @Override
            public void onSuccess(UserDetailResponseBean result) {

                if(result!=null) {
                    url=result.getData().getService_desk();
                    mUserBean = result;
                    is_store = result.getData().getIs_store();
                    is_tuidan = result.getData().getIs_tuidan();
                    initSpecial();
                    String waitSend = result.getData().getOrder_dfh();//待发货
                    String waitReceived = result.getData().getOrder_dsh();//待收货
                    String waitPay = result.getData().getOrder_wfk();//待付款
                    if(!TextUtils.isEmpty(result.getData().getNichen())) {
                        tvAccount.setText(result.getData().getNichen());
                    }else{
                        tvAccount.setText(result.getData().getPhone());
                    }
                    if(!TextUtils.isEmpty(result.getData().getType()) && result.getData().getType().equals("1")) {
                        rl_customer1.setVisibility(View.VISIBLE);
                        view_customer1.setVisibility(View.VISIBLE);
                    }else{
                        rl_customer1.setVisibility(View.GONE);
                        view_customer1.setVisibility(View.GONE);
                    }

                    tvMoney.setText(result.getData().getRevenue()+"");

                    if(!waitSend.equals("0")){
                        tvWaitSendNumber.setVisibility(View.VISIBLE);
                        tvWaitSendNumber.setText(waitSend);
                    }
                    if(!waitReceived.equals("0")){
                        tvWaitReciveNumber.setVisibility(View.VISIBLE);
                        tvWaitReciveNumber.setText(waitReceived);
                    }
                    if(!waitPay.equals("0")){
                        tvWaitPayNumber.setVisibility(View.VISIBLE);
                        tvWaitPayNumber.setText(waitPay);
                    }
                    if(!TextUtils.isEmpty(result.getData().getHeader_thumb())) {
                        Picasso.with(getActivity())
                                .load(result.getData().getHeader_thumb())
                                .fit()
                                .into(ivHeadImage);
                    }else{
                        ivHeadImage.setImageDrawable(getResources().getDrawable(R.drawable.avatar_default));
                    }

                }else{
                    UserDetailFilter filter = new UserDetailFilter((BaseParentActivity) getActivity());
                    filter.sendRequest(new BaseRequestFilterLayer.AccessResultFromFilter<UserEmpDetailResponseBean>() {
                        @Override
                        public void onSuccess(UserEmpDetailResponseBean result) {
                            url=result.getData().getService_desk();
                            is_store = result.getData().getIs_store();
                            is_tuidan = result.getData().getIs_tuidan();
                            initSpecial();
                            String waitSend = result.getData().getOrder_dfh();//待发货
                            String waitReceived = result.getData().getOrder_dsh();//待收货
                            String waitPay = result.getData().getOrder_wfk();//待付款
                            if(!TextUtils.isEmpty(result.getData().getNichen())) {
                                tvAccount.setText(result.getData().getNichen());
                            }else{
                                tvAccount.setText(result.getData().getPhone());
                            }

                            tvMoney.setText(result.getData().getRevenue()+"");

                            if(!waitSend.equals("0")){
                                tvWaitSendNumber.setVisibility(View.VISIBLE);
                                tvWaitSendNumber.setText(waitSend);
                            }
                            if(!TextUtils.isEmpty(result.getData().getType()) && result.getData().getType().equals("1")) {
                                rl_customer1.setVisibility(View.VISIBLE);
                                view_customer1.setVisibility(View.VISIBLE);
                            }else{
                                rl_customer1.setVisibility(View.GONE);
                                view_customer1.setVisibility(View.GONE);
                            }
                            if(!waitReceived.equals("0")){
                                tvWaitReciveNumber.setVisibility(View.VISIBLE);
                                tvWaitReciveNumber.setText(waitReceived);
                            }
                            if(!waitPay.equals("0")){
                                tvWaitPayNumber.setVisibility(View.VISIBLE);
                                tvWaitPayNumber.setText(waitPay);
                            }
                            if(!TextUtils.isEmpty(result.getData().getHeader_thumb())) {
                                Picasso.with(getActivity())
                                        .load(result.getData().getHeader_thumb())
                                        .fit()
                                        .into(ivHeadImage);
                            }else {
                                ivHeadImage.setImageDrawable(getResources().getDrawable(R.drawable.avatar_default));
                            }
                        }

                        @Override
                        public void onFailed(View errorLayout, RequestError serverError, int failedType) {

                        }
                    });

                }

            }

            @Override
            public void onFailed(View errorLayout, RequestError serverError, int failedType) {

            }
        });
    }

    boolean isFirst = true;
    public void initData() {
        if(!TextUtils.isEmpty(App.user.getPhoto())){
            if(isFirst){
                ivHeadImage.setImageBitmap(BitmapFactory.decodeFile(App.user.getPhoto()));
                isFirst = false;
            }
            if(!firstHeadUrl.equals(App.user.getPhoto())) {
                firstHeadUrl = App.user.getPhoto();
                ivHeadImage.setImageBitmap(BitmapFactory.decodeFile(App.user.getPhoto()));
            }
        }else {
            ivHeadImage.setImageDrawable(getResources().getDrawable(R.drawable.avatar_default));
        }
        //提示订单数量
        showOrderNumber();
    }

    public void initSpecial(){
        if (is_tuidan ==1 || is_store == 1){
            ll_special.setVisibility(View.VISIBLE);
        } else{
            ll_special.setVisibility(View.GONE);
        }
    }

    @OnClick(R.id.rl_myInfo)
    public void onClickDetail() {
        Intent intent = new Intent(getActivity(), ActivityMyProfit.class);
        startActivity(intent);
    }

    @OnClick(iv_set)
    public void onClickSet(){
        Intent intent = new Intent(getActivity(), ActivityMyinfo.class);
        startActivityForResult(intent,100);
    }

    @Override
    public void onDestroyView() {
        super.onDestroyView();
        ButterKnife.reset(this);
    }

    @OnClick({R.id.rl_1, R.id.rl_2, R.id.rl_3, R.id.rl_4, R.id.rl_5, R.id.rl_6, R.id.rl_7,
            R.id.rl_8, R.id.rl_9, R.id.rl_10,R.id.rl_11, R.id.rl_suggest, R.id.rl_aboutus,R.id.rl_rank,R.id.rl_customer,R.id.rl_score,R.id.rl_vip
    ,R.id.wp,R.id.wd,R.id.wg,R.id.rl_customer1})
    public void onClick(View view) {
        switch (view.getId()) {
            case R.id.rl_1://等待付款
                Intent intent = new Intent(getActivity(), GoPay.class);
                startActivity(intent);
                break;
            case R.id.rl_2:
                Intent waitSend = new Intent(getActivity(), WaitSendshopActivity.class);
                startActivity(waitSend);
                break;
            case R.id.rl_3:
                Intent waitReceived = new Intent(getActivity(), WaitReceivedActivity.class);
                startActivity(waitReceived);
                break;
            case R.id.rl_4:
                Intent allOrder = new Intent(getActivity(), AllOrderActivity.class);
                startActivity(allOrder);
                break;
            case R.id.rl_5:
                UIUtils.simpleStartActivity(ActivityMyLove.class);
                break;
            case R.id.rl_6:
                Intent waitRefund = new Intent(getActivity(), WaitRefundActivity.class);
                startActivity(waitRefund);
                break;
            case R.id.rl_7:
                startActivity(new Intent(context, com.easemob.helpdeskdemo.ui.LoginActivity.class).putExtra(Constant.MESSAGE_TO_INTENT_EXTRA,
                        Constant.MESSAGE_TO_PRE_SALES));
                break;
            case R.id.rl_8://优惠券
                startActivity(new Intent(getActivity(), DiscountCouponActivity.class));
                break;
            case R.id.rl_score://我的积分
                intent = new Intent(getActivity(), MyScoreActivity.class);
                if(mUserBean.getData().getHas_card() != 0){
                    intent.putExtra("score",mUserBean.getData().card_info_list.score_sy);
                }
                startActivity(intent);
                break;
            case R.id.rl_vip://VIP
                if(mUserBean.getData().getHas_card() == 0){
                    startActivity(new Intent(getActivity(), BindingVipActivity.class));
                }else {
                    intent = new Intent(getActivity(), MyVipActivity.class);
                    startActivity(intent);
                }
                break;
            case R.id.rl_9:
                startActivity(new Intent(getActivity(), BrowsingHistoryActivity.class));
                break;
            case R.id.rl_10:
                startActivity(new Intent(getActivity(), InformationSelectActivity.class));
                break;
            case R.id.rl_11:
                startActivity(new Intent(getActivity(), AllSellerActivity.class));
                break;
            case R.id.rl_rank:
                startActivity(new Intent(getActivity(),SellerTopActivity.class));
                break;
            case R.id.rl_customer:
                startActivity(new Intent(getActivity(),CustomerManageActivity.class));
                break;
            case R.id.rl_suggest:
                startActivity(new Intent(getActivity(),ActivitySuggest.class));
                break;
            case R.id.rl_aboutus:
                startActivity(new Intent(getActivity(),WebViewActivity.class)
                                .putExtra("title","关于我们")
                                .putExtra("shareType",1)
                                .putExtra("url","http://act.lolever.com/help/Explain_VerifyCode.html?type=about_us"));
                break;
            case R.id.wp:
                startActivity(new Intent(context, WPActivity.class));
                break;
            case R.id.wd:
                startActivity(new Intent(context, WDActivity.class));
                break;
            case R.id.wg:
                startActivity(new Intent(context, WGActivity.class));
                break;
            case R.id.rl_customer1:
                Intent i = new Intent(context, WebViewActivity.class);
                i.putExtra("title","顾客试衣").putExtra("url",url);
                i.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK | Intent.FLAG_ACTIVITY_CLEAR_TOP );
                context.startActivity(i);
                break;
        }
    }
}
